package cn.org.timespace.easyframe.dao.permission.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import cn.org.timespace.easyframe.dao.EfBaseDAO;
import cn.org.timespace.easyframe.dao.permission.EfMenuShortcutDAO;
import cn.org.timespace.easyframe.model.permission.EfMenu;

/**
 * A data access object (DAO) providing persistence and search support for
 * EfMenu entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.model.EfMenu
 * @author MyEclipse Persistence Tools
 */

public class EfMenuShortcutDAOImpl extends EfBaseDAO implements EfMenuShortcutDAO {
 
	public List<EfMenu> findByCondition(String userId) {
		Session session = null;
		Query query = null;
		
		List<EfMenu> resList = new ArrayList<EfMenu>();
		try {
			String queryString ="SELECT DISTINCT A.MENU_NAME,MENU_PHOTO_URL,MENU_URL,TARGET FROM EF_MENU  A,EF_ROLE_SHORTCUTMENU B,EF_USER_ROLE  D WHERE A.MENU_ID = B.SHORTCUT_ID AND B.ROLE_ID = D.ROLE_ID AND D.USER_ID = '"+userId+"' AND A.DIC_MENU_IS_VALID = '1' AND A.MENU_URL!='#' order by A.DISPLAY_NO asc, A.MENU_ID asc ";
			
			session = getSessionFactory().openSession();
			query = session.createSQLQuery(queryString);
			EfMenu efMenu=null;
			List list = query.list();
			if(list != null && list.size() > 0){
				for(int i=0;i<list.size();i++){
					Object[] obj=(Object[])list.get(i);
					efMenu=new EfMenu();
					efMenu.setMenuName((String)obj[0]);
					efMenu.setMenuPhotoUrl((String)obj[1]);
					efMenu.setMenuUrl((String)obj[2]);
					efMenu.setTarget((String)obj[3]);
					resList.add(efMenu);
				}
			}
			return resList;
			
		} catch (RuntimeException re) {
			log.error("findTopLevelMenu failed", re);
			throw re;
		}finally{
			if(session != null) session.close();
			if(query != null) query = null;
		}
	}
}